package command;

import object.Blob;
import object.Commit;
import object.Stage;
import object.Tree;

import java.util.Iterator;
import java.util.Map;

public class LsFileCommand {
    protected String type;
    protected String ID;
    public LsFileCommand(String type, String ID){
        this.type = type;
        this.ID = ID;
    }
    public LsFileCommand(String type){
        this.type = type;
        this.ID = null;
    }
    public void excute() throws Exception{
        if( "-b".equals(type) ){
            Blob blob = Blob.deserialize(this.ID);
            System.out.println("Object type");
            System.out.println(blob.getFmt());
            System.out.println("-------------------------------------------------");
            System.out.println("Object content");
            System.out.print(blob.getContent());
            System.out.println("-------------------------------------------------");
            System.out.println("Object hash key");
            System.out.println(blob.getKey());
            System.out.println("-------------------------------------------------");
            System.out.println("Object path");
            System.out.println( blob.getPath());
        }
        if("-t".equals(type)){
            Tree tree = Tree.deserialize(this.ID);
            System.out.println("Object type");
            System.out.println(tree.getFmt());
            System.out.println("-------------------------------------------------");
            System.out.println("Object content");
            System.out.print(tree.getContent());
            System.out.println("-------------------------------------------------");
            System.out.println("Object hash key");
            System.out.println(tree.getKey());
            System.out.println("-------------------------------------------------");
            System.out.println("Object path");
            System.out.println( tree.getPath());
        }
        if("-c".equals(type)){
            Commit commit = Commit.deserialize(this.ID);
            System.out.println("Object type");
            System.out.println(commit.getFmt());
            System.out.println("-------------------------------------------------");
            System.out.println("Object content");
            System.out.print(commit.getContent());
            System.out.println("-------------------------------------------------");
            System.out.println("Object hash key");
            System.out.println(commit.getKey());
            System.out.println("-------------------------------------------------");
            System.out.println("Object parent");
            System.out.println(commit.getParent());
        }
        if("-stage".equals(type)){
            Stage stage = Stage.deserialize();
            for(Map.Entry<String, String[]> entry:stage.getAddedFiles().entrySet()){
                String name = entry.getKey();
                String[] content = entry.getValue();
                System.out.println(name + " " + content[0] + " " + content[1] + " " + content[2]);
            }
        }

    }
}
